package cx3k.dataaccess.search;

import java.util.ArrayList;
import java.util.Collection;


public class RootSearchNode extends SearchNode {
	
	private class StrValue{
		
		private String name;
		private String join;
		private String path;
		
		public StrValue(String name, String join, String path){
			this.name = name;
			this.join = join;
			this.path = path;
		}
		
		public StrValue(String name, String join){
			this.name = name;
			this.join = join;
			this.path = null;
		}
		
		public String toHQL(){
			if(this.path == null){
				return join + name + " as " + name.toLowerCase();
			} else {
				return join + path + " as " + name.toLowerCase();
			}
		}
		
	}

	/*private static final String PLACE_HOLDER = "~";*/
	
	/*private static final int CLASS_NAME_IDX = 1;
	private static final int NICK_NAME_IDX = 3; 
	
	private static final String[] FROM = {"FROM ", PLACE_HOLDER, " as ", PLACE_HOLDER};*/	
	
	private static final String WHERE = " WHERE ";
	
	private Collection<StrValue> classNames;
	
	public void join(RootSearchNode other, String path){
//		classNames.add(" join " + other.getClassName());
		classNames.add(new StrValue(other.getClassName(), "", path));
		
		
		
		if(!getChildren().isEmpty()){
			SearchValue old;
			SearchValue add;
			
			old = other.getChildren().toArray(new SearchValue[other.getChildren().size()])[0];
			add = new AndSearchValue(old.getClassName(), old.getFieldName(), old.getValue(), old.getOperator(), this);
			
			other.getChildren().remove(old);
//			other.getChildren().add(add);
			
			super.addChild(add);
			
			for(SearchNode node : other.getChildren()){
//				if(!node.getParent().equals(this)){
					node.setParent(add);
					add.addChild(node);
//				}
			}
			
			
			
			/*for(SearchNode node : other.getChildren()){
				node.setParent(this);
				super.addChild(node);
			}*/
			return;
			
		}
		
		
		
		for(SearchNode node : other.getChildren()){
			node.setParent(this);
			super.addChild(node);
		}
	}
	
	public void and(RootSearchNode other){
//		classNames.add(", " + other.getClassName());
		classNames.add(new StrValue(other.getClassName(), ", "));
		
		if(!getChildren().isEmpty()){
			SearchValue old = other.getChildren().toArray(new SearchValue[other.getChildren().size()])[0];
			AndSearchValue add = new AndSearchValue(old.getClassName(), old.getFieldName(), old.getValue(), old.getOperator(), this);
			
			other.getChildren().remove(old);
			other.getChildren().add(add);
		}
		
		for(SearchNode node : other.getChildren()){
			node.setParent(this);
			super.addChild(node);
		}
	}
	
	public void or(RootSearchNode other){
//		classNames.add(", " + other.getClassName());
		classNames.add(new StrValue(other.getClassName(), ", "));
		
		if(!getChildren().isEmpty()){
			SearchValue old = other.getChildren().toArray(new SearchValue[other.getChildren().size()])[0];
			OrSearchValue add = new OrSearchValue(old.getClassName(), old.getFieldName(), old.getValue(), old.getOperator(), this);
			
			other.getChildren().remove(old);
			other.getChildren().add(add);
		}
		
		for(SearchNode node : other.getChildren()){
			node.setParent(this);
			super.addChild(node);
		}
	}
	
	public RootSearchNode(Class<?> type){
		super(type);
		super.setParent(null);
		init();
	}
	
	public RootSearchNode(String className) {
		super(className);
		super.setParent(null);
		init();
	}

	private void init(){
		classNames = new ArrayList<StrValue>();
		classNames.add(new StrValue(super.getClassName(), ""));
	}
	
	@Override
	public String getDefaultQueryStr() {
		
		StringBuffer query = new StringBuffer(createSelectQuery());
		
		if(!getChildren().isEmpty()){
			query.append(WHERE);
			
			for(SearchNode child : getChildren()){
				query.append(child.getDefaultQueryStr());
			}
		}
		
		return query.toString();
	}
	
	private String createSelectQuery(){
		
		StringBuffer query = new StringBuffer();
		
		query.append("FROM");
		query.append(" ");
//		int count = 0;
		
		for(StrValue name : classNames){
			/*if(count++ > 0){
				query.append(" join ");
			}*/
			/*query.append(name);
			query.append(" as ");
			query.append(name.toLowerCase());*/
			query.append(name.toHQL());
		}
		
//		query.append(" ");
		
		return query.toString();
		
		/*for(String statement : createSelectQueryArray()){
			query.append(statement);
			query.append(" ");
		}
		
		return query.toString();*/
	}
	
	/*private String[] createSelectQueryArray(){
		
		String[] query = FROM;
		
		query[CLASS_NAME_IDX] = super.getClassName();
		query[NICK_NAME_IDX] = super.getNickName();
		
		return query;
	}*/

	
}
